/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.prototype.console;

/**
 *
 * @author zineddine
 */
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 *
 * @author zineddine
 */

public class JobThread implements Callable<ConcurrentHashMap<String, String>> {
    /**.
     * @param concurrentMap
     */
    private ConcurrentHashMap<String, String> concurrentMap;
    /**.
     * @param pageLink
     */
    private String pageLink;
    /**.
     * @param sfPage
     */
    private Document sfPage;
    /**.
     * Constructor
     * @param newLinkPage
     */
    public JobThread(final String newLinkPage) {
        this.concurrentMap = null;
        this.sfPage = null;
        this.pageLink = newLinkPage;
    }

    /**.
     * @return concurrentMap
     */
    public ConcurrentHashMap<String, String> getConcurrentMap() {
        return concurrentMap;
    }

    @Override
    public ConcurrentHashMap<String, String>  call() {
        try {
            concurrentMap = new ConcurrentHashMap<String, String>();
            sfPage = Jsoup.connect(pageLink).timeout(0).get();
            Element resultat = sfPage.getElementById("result_data");
            Elements links = resultat.getElementsByTag("a");

            for (Element link : links) {
                String linkHref = link.attr("href");
                String linkText = link.text();

                concurrentMap.put(linkText, linkHref);
            }
        } catch (Exception ex) {
            ex.getMessage();
        }
        return concurrentMap;
    }
}